package logic

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"MyBlogv2/Servers/UserAction/internal/svc"
	"MyBlogv2/Servers/UserAction/userAction"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCommentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCommentLogic {
	return &AddCommentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddCommentLogic) AddComment(in *UserAction.UserActionRequest) (*UserAction.CommonResponse, error) {
	var comment models.Comment
	userId := int(in.UserId)
	userCommentDto := in.UserCommentDto

	if userCommentDto.ParentId == 0 {
		userCommentDto.ParentId = userCommentDto.ReplyId
	}
	copier.Copy(&comment, &userCommentDto)
	comment.CommentUserId = userId
	err := models.Db.Model(new(models.Comment)).Create(&comment).Error
	if err != nil {
		return &UserAction.CommonResponse{
			Code: int64(HttpCode.FAILURE),
		}, nil
	}

	var user models.User
	models.Db.Model(new(models.User)).Where("id = ?", userId).First(&user)
	if user.Email == "" {
		return &UserAction.CommonResponse{
			Code: int64(HttpCode.SUCCESS),
			Msg:  "检测到您尚未绑定邮箱,无法开启邮箱提醒，请先绑定邮箱",
		}, nil
	}
	return &UserAction.CommonResponse{
		Code: int64(HttpCode.SUCCESS),
	}, nil
}
